package com.it.controller;

import com.it.VO.MessageCommentOV;
import com.it.common.Result;
import com.it.domain.MessageComment;
import com.it.domain.User;
import com.it.service.LoginService;
import com.it.service.MessageCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/MessageComment")
public class MessageCommentController {

    @Autowired
    private MessageCommentService messageCommentService;

    @Autowired
    private LoginService loginService;

    @PostMapping("/saveComment")
    public Object saveComment(@RequestBody MessageComment messageComment ,HttpServletRequest request){

        if(messageComment.getParentId() == null){
            messageComment.setParentId(-1);
        }
        if(messageComment.getIsDelete() == null){
            messageComment.setIsDelete((byte) 0);
        }
        if(messageComment.getRootId() == null){
            messageComment.setRootId(-1);
        }
        System.out.println(messageComment.getContent());
        String content = messageComment.getContent().trim();
        if(content == null || content == ""){
            return Result.fail("404","内容为空");
        }

        String token = request.getHeader("token");
        User user = loginService.CheckToken(token);
        if(user == null){
            return Result.fail("404","登录过期");
        }
        messageComment.setUserId(user.getId());
        if(messageCommentService.saveComment(messageComment))
            return Result.success("200","评论成功");
        else
            return Result.fail("404","评论失败");
    }

//    获取根评论
    @GetMapping("/getCommentList")
    public Object getCommentList(){

        System.out.println(12213);
        List<MessageCommentOV> messageCommentOVList = messageCommentService.SelectAll();
        if(messageCommentOVList == null){
            System.out.println("null");
            return null;
        }
        Map<Integer, MessageCommentOV> map = new HashMap<>();
        Map<Integer, MessageCommentOV> result = new HashMap<>();

        List<MessageCommentOV> data = new ArrayList<>();
        for(MessageCommentOV msgComment : messageCommentOVList){
            if(msgComment.getRootId() == -1){//找根评论
                result.put(msgComment.getId(),msgComment);
            }
            map.put(msgComment.getId(),msgComment);
        }

        for(MessageCommentOV msgComment : messageCommentOVList){
            Integer id = msgComment.getRootId();
            if(id != -1) {//根评论下所有子评论
                if (result.get(id).getChildren() == null) {
                    result.get(id).setChildren(new ArrayList<>());
                }
                result.get(id).getChildren().add(msgComment);
            }
        }

        for(MessageCommentOV msgComment : messageCommentOVList){
            if(msgComment.getRootId() == -1){//找根评论
                data.add(result.get(msgComment.getId()));
            }
        }
        return data;
    }

    @PostMapping("/DeleteComment")
    public Object DeleteComment(@RequestBody MessageComment messageComment){

        Queue<MessageComment> queue = new LinkedList<>();

        if(messageComment.getRootId() == -1){
            System.out.println(messageCommentService.DeleteComment(messageComment.getId()));
            System.out.println(messageCommentService.DeleteRootComment(messageComment.getId()));
        }
        else{
            queue.offer(messageComment);
            while(queue.peek() != null){
                MessageComment msg1 = queue.peek();
                List<MessageComment> messageCommentList = messageCommentService.SelectByParentId(msg1.getId());
                messageCommentService.DeleteComment(msg1.getId());
                queue.poll();
                for(MessageComment messageComment1 : messageCommentList){
                    if(messageComment1 != null){ 
                        queue.offer(messageComment1);
                    }
                }
            }
        }
        System.out.println("删除成功");
        return Result.success("200","删除成功");
    }
}
